<?php

namespace app\tools;

use Yii;

class SMS
{
    public static function send($phone, $code, $temp_id) {

      $params = array ();

      // *** 需用户填写部分 ***
      // fixme 必填：是否启用https
      $security = false;

      // fixme 必填: 请参阅 https://ak-console.aliyun.com/ 取得您的AK信息
      $accessKeyId = "LTAI4G4Km1iocFF1AzW8T5HF";
      $accessKeySecret = "x3jHeXOPUAxankM7tLujdPegy94o5J";

      // fixme 必填: 短信接收号码
      $params["PhoneNumbers"] = $phone;

      // fixme 必填: 短信签名，应严格按"签名名称"填写，请参考: https://dysms.console.aliyun.com/dysms.htm#/develop/sign
      $params["SignName"] = "润源";

      // fixme 必填: 短信模板Code，应严格按"模板CODE"填写, 请参考: https://dysms.console.aliyun.com/dysms.htm#/develop/template
      $params["TemplateCode"] = $temp_id;

      // fixme 可选: 设置模板参数, 假如模板中存在变量需要替换则为必填项
      if ($temp_id == 'SMS_219750003') {
        $code = [
          "title" => $code
        ];
      } else {
        $code = [
          "code" => $code
        ];
      }
      $params['TemplateParam'] = json_encode($code);
      //var_dump($params);die;
      // 初始化SignatureHelper实例用于设置参数，签名以及发送请求
      $helper = new SignatureHelper();

      // 此处可能会抛出异常，注意catch
      $content = $helper->request(
          $accessKeyId,
          $accessKeySecret,
          "dysmsapi.aliyuncs.com",
          array_merge($params, array(
              "RegionId" => "cn-hangzhou",
              "Action" => "SendSms",
              "Version" => "2017-05-25",
          )),
          $security
      );
      return $content;
  }
}
